package com.lrs.java.jdbc.jdbc_teacher;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//树状展现,需提前准备表数据
class OracleArticleTree {
	public static void main(String[] args) {
		Connection conn = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			conn = DriverManager.getConnection(
					"jdbc:oracle:thin:@localhost:1521:BJSXT", "scott", "tiger");
			tree(conn, 0, 0);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (conn != null) {
					conn.close();
					conn = null;
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}

	private static void tree(Connection conn, int id, int grade) {
		String preStr = "";
		for (int i = 0; i < grade; i++) {
			preStr += "----";
		}

		String sql = "select * from article where pid = " + id;
		Statement stmt = null;
		ResultSet rs = null;

		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql);
			while (rs.next()) {
				System.out.println(preStr + rs.getString("cont"));
				if (rs.getInt("isleaf") == 0) {
					tree(conn, rs.getInt("id"), grade + 1);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}

			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

	}
}





